import random
from address import Address
from account import Account


class Bank:
    def __init__(self, name):
        self.name = name
        self.account = {}

    def add_account(self):
        if len(self.account) < 100:
            username = random.randint(10000000, 99999999)
            if username not in self.account:
                name = input('请输入用户姓名：')
                password = int(input('请输入六位数字密码：'))
                province = input('请输入居住地省份：')
                city = input('请输入居住城市：')
                street = input('请输入详细地址：')
                address = Address(province, city, street)
                self.account[username] = Account(username, name, password, address, 0, name)
                print('开户成功，用户名：%s' % username)
                return 1
            else:
                print('开户失败，用户已存在')
                return 2
        else:
            print('开户失败，用户库已达上限')
            return 3

    def get_account(self, username):
        if username in self.account:
            password = int(input('请输入六位数字密码：'))
            if password == self.account[username].password:
                print(self.account[username])
            else:
                print('密码错误')
                return 2
        else:
            print("No such account")
            return 1

    def del_account(self, username):
        if username in self.account:
            del self.account[username]
        else:
            print("No such account")

    def save_money(self, username):
        if username in self.account:
            money = int(input('请输入存款金额：'))
            self.account[username].money += money
            return True
        return False

    def withdrawal(self, username):
        if username in self.account:
            password = int(input('请输入六位数字密码：'))
            if password == self.account[username].password:
                balance = self.account[username].balance
                money = input('请输入取款金额：')
                if balance >= money:
                    self.account[username].balance -= money
                    return 4
                else:
                    print('钱不够')
                    return 3
            else:
                print('密码错误')
                return 2
        else:
            print("No such account")
            return 1

    def transfer(self, username):
        if username in self.account:
            password = int(input('请输入六位数字密码：'))
            if password == self.account[username].password:
                target = input('请输入目标账户：')
                if target in self.account:
                    balance = self.account[username].balance
                    money = input('请输入转帐金额：')
                    if balance >= money:
                        self.account[username].balance -= money
                        self.account[target].balance += money
                        return 0
                    else:
                        print('钱不够')
                        return 3
                else:
                    print('目标账户不存在')
                    return 1
            else:
                print('密码错误')
                return 2
        else:
            print("No such account")
            return 1
